package net.dodoman.pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class DynamicProxyClient {

	public static void main(String[] args) throws Throwable {
		//被代理对象
		RealSubject rs = new RealSubject();
		//代理类,实现InvocationHandler,依赖真实对象
		InvocationHandler ih = new DynamicProxySubject(rs);
		Class<?> cls = rs.getClass();
		//真实对象实例(实现被动态代理的接口,依赖InvocationHandler)
		Subject subject = (Subject) Proxy.newProxyInstance(
				cls.getClassLoader(), cls.getInterfaces(), ih);
		
		Class<?> Anotherls = rs.getClass();
		subject.request();
		// 另外一个动态代理
		InvocationHandler anotherIh = new AnotherDynamicProxySubject(rs);
		Subject anotherSubject = (Subject) Proxy.newProxyInstance(
				Anotherls.getClassLoader(), Anotherls.getInterfaces(),
				anotherIh);
		anotherSubject.request();
	}
}
